package com.taoban.shopdata.entity;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlRootElement;

import com.sun.xml.internal.txw2.annotation.XmlElement;
import com.yufei.entity.Entity;
import com.yufei.entity.PaginationRule;
import com.yufei.utils.CommonUtil;

/**
 * @author jasstion
 * 上午10:00:56
 * 用于采集天猫商城上面的商品数据的配置信息比如:分类查询对应的url，以及mallItemId对应的正则表达式等等
 */
@XmlRootElement
public class TMallCrawlerConfig extends Entity{
	
private String keywords=null;
//对应的分类
private String catalogId;


private PaginationRule paginationRule=null;

private String searchUrl;
/**
 * http://list.tmall.com/search_product.htm?fromF5=&q=%C4%D0%CA%BF%CD%E2%CC%D7&totalPage=100&cat=50025174&sort=s&style=g&vmarket=0&from=xl_1_suggest&suggest=0_1&area_code=110000&jumpto={}
注意把jumpto=1改成jumpto={}
 */
private String pageUrlTemplate;
public String getPageTotalNumberRegex() {
	return pageTotalNumberRegex;
}
@XmlElement
public void setPageTotalNumberRegex(String pageTotalNumberRegex) {
	this.pageTotalNumberRegex = pageTotalNumberRegex;
}
private String itemIdRegex;
private String pageTotalNumberRegex;
public TMallCrawlerConfig() {
	super();
	// TODO Auto-generated constructor stub
}
public String getSearchUrl() {
	return searchUrl;
}
@XmlElement
public void setSearchUrl(String searchUrl) {
	this.searchUrl = searchUrl;
}
public String getPageUrlTemplate() {
	return pageUrlTemplate;
}
@XmlElement
public void setPageUrlTemplate(String pageUrlTemplate) {
	this.pageUrlTemplate = pageUrlTemplate;
}
public String getItemIdRegex() {
	return itemIdRegex;
}
@XmlElement
public void setItemIdRegex(String itemIdRegex) {
	this.itemIdRegex = itemIdRegex;
}
public static boolean  isValid(TMallCrawlerConfig tMallCrawlerConfig){
	boolean isValid=true;
	if(CommonUtil.isEmptyOrNull(tMallCrawlerConfig.searchUrl)||CommonUtil.isEmptyOrNull(tMallCrawlerConfig.getPageUrlTemplate())||!tMallCrawlerConfig.getPageUrlTemplate().contains("{}")){
		isValid=false;
	}
return isValid;
}
public String getKeywords() {
	return keywords;
}
public PaginationRule getPaginationRule() {
	return paginationRule;
}
public void setPaginationRule(PaginationRule paginationRule) {
	this.paginationRule = paginationRule;
}
@XmlElement
public void setKeywords(String keywords) {
	this.keywords = keywords;
}


public String getCatalogId() {
	return catalogId;
}
public void setCatalogId(String catalogId) {
	this.catalogId = catalogId;
}
public static void main(String[] args){
	List<TMallCrawlerConfig> tMallCrawlerConfigs=new ArrayList<>();
	TMallCrawlerConfig tMallCrawlerConfig =new TMallCrawlerConfig();
	tMallCrawlerConfig.setCatalogId("181");
	tMallCrawlerConfig.setKeywords("笔记本");
	tMallCrawlerConfig.setSearchUrl("http://list.tmall.com//search_product.htm?q=%C4%D0%CA%BF%CD%E2%CC%D7&type=p&style=&cat=all&xl=%C4%D0_1&from=xl_1_suggest");
	tMallCrawlerConfig.setPageUrlTemplate("http://list.tmall.com/search_product.htm?fromF5=&q=%C4%D0%CA%BF%CD%E2%CC%D7&totalPage=100&cat=50025174&sort=s&style=g&vmarket=0&from=xl_1_suggest&suggest=0_1&area_code=110000&jumpto={}");
	tMallCrawlerConfig.setPageTotalNumberRegex("ui-page-s-len\">(.*?)</b>");
	tMallCrawlerConfig.setItemIdRegex("data-id=\"(.*?)\"");
	tMallCrawlerConfigs.add(tMallCrawlerConfig);
	TMallCrawlerConfigList tMallCrawlerConfigList=new TMallCrawlerConfigList();
	tMallCrawlerConfigList.settMallCrawlerConfig(tMallCrawlerConfigs);
	System.out.print(CommonUtil.generateXmlForObject(tMallCrawlerConfigList, TMallCrawlerConfigList.class));
	
}
}
